
/***************************************************************
 * sep_bcc.c
 *
 * Build a bcc lattice and write to file start.xyz
 *
 * usage: sep_bcc <nx> <ny> <nz> <gabx> <gaby> <gabz> <type>
 *
 * Author: Jesper Hansen
 ***************************************************************/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define VERSION "0.1"

void printpre( void );
void checkfile( void );

int main( int argc, char **argv ){
  int numb[3];
  float gab[3], lbox[3];

  if ( argc != 8 ){
    fprintf(stderr, 
	    "Usage: sep_bcc <nx> <ny> <nz> <gabx> <gaby> <gabz> <type>\n");
    exit(EXIT_FAILURE);
  }

  printpre();
  checkfile();

  for ( int k=0; k<3; k++ ){
    numb[k] = atoi(argv[k+1]);
    gab[k]  = atof(argv[k +4]);
    lbox[k] = numb[k]*gab[k];
  }

  char type = argv[7][0];

  FILE *fout = fopen("start.xyz", "w");
  if ( fout == NULL ) {
    fprintf(stderr, "Coulnd't open file start.xyz");
    exit(EXIT_FAILURE);
  }
  
  fprintf(fout, "%d\n", numb[0]*numb[1]*numb[2]);
  fprintf(fout, "%f %f %f\n", lbox[0], lbox[1], lbox[2]);

  float c[3];
  int ngab, ngab0 = 0; 
  for (int nZ = 0; nZ < numb[2]; nZ++ ){
    c[2] = nZ*gab[2];
    ngab = ngab0;
    for ( int nY = 0; nY < numb[1]; nY++ ){
      c[1] = nY*gab[1];
      for ( int nX = 0; nX < numb[0]; nX++ ){
	c[0] = nX*gab[0];
	if (ngab == 1) c[0] += 0.5*gab[0];
	
	fprintf(fout,"%c %f %f %f 0.0 0.0 0.0 1.0 0.0\n", 
		type, c[0], c[1], c[2]); 
      }
      if (ngab == 0)
	ngab = 1;
      else 
	ngab = 0;
    }
    if (ngab0 == 0)
      ngab0 = 1;
    else 
      ngab0 = 0;
  }

  fclose(fout);
 
  printf("Wrote xyz configurational file: start.xyz\n");
 
  return 0;
}


void printpre( void ){

  printf("This is sep_bcc version %s\n", VERSION);
  printf("This program is a part of the sep library - there is absolutely ");
  printf("NO WARRANTY\n\n");

  
}


void checkfile( void ){

  FILE *fout = fopen("start.xyz", "r");
  if ( fout != NULL ){
    fprintf(stderr, "Warning: ");
    fprintf(stderr, "start.xyz exists - it will be overwritten\n");
    fclose(fout);
  }

}
